﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using FSM_Framework.StateMachine;

namespace FSM_Framework
{
    //todo:设计流程和状态机的关系及交互 BladeDeviceSystem依赖流程，流程依赖IBladeDeviceSystem, IBladeOperation接口
    //todo: 添加状态驱动的标准workflow
    /// <summary>
    /// 整机系统 IBladeOperation接口可暴露给其他业务系统
    /// </summary>
    public class BladeDeviceSystem : IBladeDeviceSystem
    {
        public BladeDeviceSystem()
        {
            _stateMachine = new StateMachine.StateMachine<Operation, BladeState>(BladeState.Init);
            RegisterOperations();
            RegisterConditions();

        }

        private void RegisterOperations()
        {

            _stateMachine.RegisterOperation(BladeState.Idle, Operation.Start, BladeState.Ready);
            _stateMachine.RegisterOperation(BladeState.Idle, Operation.Stop, BladeState.Stopping);

            _stateMachine.RegisterOperation(BladeState.Ready, Operation.Stop, BladeState.Stopping);
            _stateMachine.RegisterOperation(BladeState.Stopping, Operation.Start, BladeState.Idle);

        }
        private void RegisterConditions()
        {
            _stateMachine.RegisterCondition(BladeState.Init, new AutoTransitionCondition<BladeState>()
            {

                NextBladeState = BladeState.Idle,

                TransitionAction = new TransitionAction()
                {
                    Execute = p =>
                    {
                        Console.WriteLine("正在初始化");
                        Thread.Sleep(3000);
                        Console.WriteLine("初始化完成,自动迁移到Idle状态");
                    },
                    CanExecute = p => true,
                }
            });

            _stateMachine.RegisterCondition(BladeState.Stopping, new AutoTransitionCondition<BladeState>()
            {

                NextBladeState = BladeState.Idle,

                TransitionAction = new TransitionAction()
                {
                    Execute = p =>
                    {
                        Console.WriteLine("正在初始化");
                        Thread.Sleep(3000);
                        Console.WriteLine("初始化完成,自动迁移到Idle状态");
                    },
                    CanExecute = p =>
                    {

                        Console.WriteLine("正在进行设备检查....");
                        Thread.Sleep(1500);
                        Console.WriteLine("设备正常");
                        return true;
                    },
                }
            });
        }

        private StateMachine.StateMachine<Operation, BladeState> _stateMachine { get; }


        public async Task Start()
        {
            var res = CanExecuteStarting();
            if (res == false) throw new InvalidOperationException("无法执行");
            await _stateMachine.ExecuteAsync(Operation.Start, () =>
            {
                //业务代码
            });
        }

        public bool CanExecuteStarting()
        {
            return _stateMachine.CanExecute(Operation.Start); ;
        }

        public Task Pause()
        {
            return null;
        }

        public Task<bool> CanExecutePausing()
        {
            return null;
        }

        public async Task Stop()
        {
            await _stateMachine.ExecuteAsync(Operation.Stop, () =>
            {
                //业务代码
                Console.WriteLine("正在停止...");
                Thread.Sleep(3000);
                //停止efem子系统
                //停止运动子系统
            });
        }

        public Task SwitchToManual()
        {
            return null;
        }

        public Task SwitchToAuto()
        {
            return null;
        }

        public event EventHandler<StateChangedArgs<BladeState>> StateChanged
        {
            add => _stateMachine.StateChanged += value;
            remove => _stateMachine.StateChanged -= value;
        }
        public event EventHandler<bool> IsBusyChanged
        {
            add => _stateMachine.IsBusyChanged += value;
            remove => _stateMachine.IsBusyChanged -= value;
        }
        public bool IsBusy => _stateMachine.IsBusy;
        public BladeState BladeState => _stateMachine.State;
    }


}
